<template>
  <view class="custom-tab-bar">
    <view class="tab-bar-border"></view>
    <view 
      v-for="(item, index) in tabList" 
      :key="index"
      class="tab-bar-item"
      :class="{ 'tab-bar-item--active': selected === index }"
      @tap="switchTab(item, index)"
    >
      <!-- 中间的回收按钮特殊处理 -->
      <view v-if="index === 2" class="recycle-button">
        <view class="recycle-icon">
          <text class="recycle-text">♻</text>
        </view>
      </view>
      
      <!-- 普通导航项 -->
      <template v-else>
        <view class="tab-icon">
          <text class="icon-text">{{ getIconText(item, index) }}</text>
        </view>
        <view class="tab-text" :style="{ color: selected === index ? selectedColor : color }">
          {{ item.text }}
        </view>
      </template>
    </view>
  </view>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue'

const selected = ref(0)
const color = ref('#999999')
const selectedColor = ref('#8BC34A')

const tabList = ref([
  {
    pagePath: '/pages/index/index',
    text: '首页'
  },
  {
    pagePath: '/pages/news/news',
    text: '动态'
  },
  {
    pagePath: '/pages/recycle/recycle',
    text: '回收'
  },
  {
    pagePath: '/pages/mall/mall',
    text: '商城'
  },
  {
    pagePath: '/pages/profile/profile',
    text: '我的'
  }
])

const getIconText = (item, index) => {
  const icons = ['🏠', '📰', '♻', '🛒', '👤']
  return icons[index] || '•'
}

const switchTab = (item, index) => {
  selected.value = index
  uni.switchTab({
    url: item.pagePath
  })
}

// 暴露方法给父组件调用
const setSelected = (index) => {
  selected.value = index
}

defineExpose({
  setSelected
})
</script>

<style lang="scss" scoped>
.custom-tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120rpx;
  background: #ffffff;
  display: flex;
  align-items: center;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 1000;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1);
}

.tab-bar-border {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2rpx;
  background: rgba(0, 0, 0, 0.1);
}

.tab-bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  height: 100%;
  transition: all 0.3s ease;
}

.tab-bar-item--active {
  .tab-text {
    font-weight: 600;
  }
}

.recycle-button {
  width: 112rpx;
  height: 112rpx;
  background: linear-gradient(135deg, #8BC34A 0%, #7CB342 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: -32rpx;
  box-shadow: 0 8rpx 24rpx rgba(139, 195, 74, 0.4);
  border: 6rpx solid #ffffff;
  transition: all 0.3s ease;
}

.recycle-button:active {
  transform: scale(0.95);
  box-shadow: 0 4rpx 16rpx rgba(139, 195, 74, 0.4);
}

.recycle-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.recycle-text {
  font-size: 56rpx;
  color: #ffffff;
  font-weight: bold;
}

.tab-icon {
  margin-bottom: 8rpx;
}

.icon-text {
  font-size: 44rpx;
  line-height: 1;
}

.tab-text {
  font-size: 20rpx;
  line-height: 1;
  transition: all 0.3s ease;
}
</style>